﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using FileMonitor.Service.ProcessMonitor.Model;
using FileMonitor.Service.ProcessMonitor.Contract;
using FileMonitor.Service.Provider.FileMonitor;

namespace FileMonitor.Client
{
    public partial class ProcessForm : UserControl
    {
        public ProcessForm()
        {
            InitializeComponent();
            this.gridErrorFile.AutoGenerateColumns = false;
            this.gridToBeProcessFile.AutoGenerateColumns = false;
            this.gridProcessedFile.AutoGenerateColumns = false;
            this.gridNonProcessBindingFile.AutoGenerateColumns = false;

        }

        private string endPonitName = "";

        public void Initialize(MonitorProcessInfo monitorProcessInfo,string endPointName)
        {
            if (monitorProcessInfo != null)
            {
                this.lblProcessName.Text = monitorProcessInfo.ProcessName;
                this.lblMonitorPath.Text = monitorProcessInfo.MonitorPath;
                this.lblState.Text = monitorProcessInfo.State.ToString();
                this.lblDesp.Text = monitorProcessInfo.ProcessDescription;
                this.endPonitName = endPointName;
                if (monitorProcessInfo.State == ProcessState.Pause)
                {
                    this.btnStart.Visible = true;
                    this.btnStop.Visible = false;
                }
                else if (monitorProcessInfo.State == ProcessState.Running)
                {
                    this.btnStart.Visible = false;
                    this.btnStop.Visible = true;
                }
                BindFileInfo(monitorProcessInfo.ProcessedFileInfos);
            }
            else
            {
                this.lblProcessName.Text = string.Empty;
                this.lblMonitorPath.Text = string.Empty;
                this.lblState.Text = string.Empty;
                this.lblDesp.Text = string.Empty;
                this.endPonitName = endPointName;
                this.gridToBeProcessFile.DataSource = null;
                this.gridProcessedFile.DataSource = null;
                this.gridErrorFile.DataSource = null;
                this.gridNonProcessBindingFile.DataSource = null;
                this.btnStart.Visible = false;
                this.btnStop.Visible = false;
            }
        }

        private void BindFileInfo(List<FileInfo> fileInfos)
        {
            this.gridToBeProcessFile.DataSource = fileInfos.Where(fi => fi.State == FileState.ToBeProcess).ToList();
            this.gridProcessedFile.DataSource = fileInfos.Where(fi => fi.State == FileState.Processed).ToList();
            this.gridErrorFile.DataSource = fileInfos.Where(fi => fi.State == FileState.Error).ToList();
            this.gridNonProcessBindingFile.DataSource = fileInfos.Where(fi => fi.State == FileState.NonProcessBinding).ToList();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string currentProcessName = this.lblProcessName.Text;
            if (!string.IsNullOrEmpty(currentProcessName))
            {
                using (ChannelFactory<IMonitorProcessService> channelFactory = new ChannelFactory<IMonitorProcessService>(endPonitName))
                {
                    IMonitorProcessService channel = channelFactory.CreateChannel();
                    channel.StartProcess(currentProcessName);
                }
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            string currentProcessName = this.lblProcessName.Text;
            if (!string.IsNullOrEmpty(currentProcessName))
            {
                using (ChannelFactory<IMonitorProcessService> channelFactory = new ChannelFactory<IMonitorProcessService>(endPonitName))
                {
                    IMonitorProcessService channel = channelFactory.CreateChannel();
                    channel.StopProcess(currentProcessName);
                    Dictionary<string, MonitorProcessInfo> mis = channel.GetAllProcessInfo();
                    if (mis.ContainsKey(currentProcessName))
                    {
                        Initialize(mis[currentProcessName], endPonitName);
                    }
                    else
                    {
                        Initialize(null, endPonitName);
                    }
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            string currentProcessName = this.lblProcessName.Text;
            if (!string.IsNullOrEmpty(currentProcessName))
            {
                using (ChannelFactory<IMonitorProcessService> channelFactory = new ChannelFactory<IMonitorProcessService>(endPonitName))
                {
                    IMonitorProcessService channel = channelFactory.CreateChannel();
                    Dictionary<string, MonitorProcessInfo> mis = channel.GetAllProcessInfo();
                    if (mis.ContainsKey(currentProcessName))
                    {
                        Initialize(mis[currentProcessName], endPonitName);
                    }
                    else
                    {
                        Initialize(null, endPonitName);
                    }
                }
            }
        }
    }
}
